package com.yenlien.traceyun.plant.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.plant.entity.Reap;
import com.yenlien.traceyun.plant.service.ReapService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 采收
 *
 * @author maybe
 * @date 2020年7月27日
 */
@RestController
@RequestMapping("/reap")
public class ReapController extends BaseController {
    @Autowired
    private ReapService reapService;

    //列表查询
//	@DataAuth
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody JSONObject params) {
        if (StringUtils.isEmpty(params.getString("companyId"))) {
            params.put("companyId", UserAuthUtil.getLoginUser().getCompanyId());
        }
        return reapService.findByList(params);
    }


    @RequestMapping(value = "/findByListApp", method = RequestMethod.POST)
    public Object findByListApp(@RequestBody JSONObject params) {
        if (StringUtils.isEmpty(params.getString("companyId"))) {
            params.put("companyId", UserAuthUtil.getLoginUser().getCompanyId());
        }
        return reapService.findByListApp(params);
    }


    // 获取当前企业种植的地块名称和基源
    @GetMapping(value = "/getSoilJY")
    public Object getSoilJY(String companyId) {
        return success(reapService.getSoilJY(StringUtils.isEmpty(companyId) ? UserAuthUtil.getLoginUser().getCompanyId(): companyId));
    }







    //根据ID查询详情
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(reapService.findById(id));
    }

    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    public Object getById(@PathVariable("id") String id) {
        return success(reapService.getById(id));
    }

    //新增
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(Reap reap, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        reapService.save(reap, files);
        return success("success");
    }

    // 果园二期
    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    public Object save2(@RequestBody Reap reap) throws Exception {
        reapService.save2(reap);
        return success("success");
    }





    /**
     * 地块农事数据回显
     *
     * @return
     */
    @PostMapping("/plantBatch")
    public Object plantBatch(@RequestBody JSONObject param) {
        return success(reapService.plantBatch(param.getString("plantBatch")));
    }
}
